package com.star.quartz.controller;

import com.star.quartz.consts.Constants;
import com.star.quartz.domain.SysJob;
import com.star.quartz.service.ISysJobService;
import com.star.quartz.util.CronUtils;
import com.star.quartz.util.StringUtils;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 调度任务信息操作处理
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/monitor/job")
public class SysJobController {
    @Autowired
    private ISysJobService jobService;


    /**
     * 新增定时任务
     */
    @PostMapping
    public String add(@RequestBody SysJob job) throws SchedulerException {

        if (!CronUtils.isValid(job.getCronExpression())) {
            return "新增任务'" + job.getJobName() + "'失败，Cron表达式不正确";
        } else if (StringUtils.containsIgnoreCase(job.getInvokeTarget(), Constants.LOOKUP_RMI)) {
            return "新增任务'" + job.getJobName() + "'失败，目标字符串不允许'rmi'调用";
        } else if (StringUtils.containsAnyIgnoreCase(job.getInvokeTarget(), new String[]{Constants.LOOKUP_LDAP, Constants.LOOKUP_LDAPS})) {
            return "新增任务'" + job.getJobName() + "'失败，目标字符串不允许'ldap(s)'调用";
        } else if (StringUtils.containsAnyIgnoreCase(job.getInvokeTarget(), new String[]{Constants.HTTP, Constants.HTTPS})) {
            return "新增任务'" + job.getJobName() + "'失败，目标字符串不允许'http(s)'调用";
        } else if (StringUtils.containsAnyIgnoreCase(job.getInvokeTarget(), Constants.JOB_ERROR_STR)) {
            return "新增任务'" + job.getJobName() + "'失败，目标字符串存在违规";
        }
//        else if (!ScheduleUtils.whiteList(job.getInvokeTarget())) {
//            return "新增任务'" + job.getJobName() + "'失败，目标字符串不在白名单内";
//        }

        jobService.insertJob(job);
        return "success";
    }

}
